package com.huitone.smspfm.core.filter;

import java.util.List;
import java.util.Set;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import com.huitone.smspfm.logger.ExtLogger;

import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.core.config.AppStaticConfigs;
import com.huitone.smspfm.core.constant.DealResult;
import com.huitone.smspfm.model.SmsEntityForMt;

/**   
* @Title AttachMsgFilterTask.java 
* @Package com.huitone.smspfm.core.task 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年6月18日 上午10:25:42 
* @version V1.0   
*/
public class AttachMsgFilterTask extends Thread {

	private ExtLogger logger = ExtLogger.create(AttachMsgFilterTask.class);
	
	private String moduleName = "附加短信过滤模块";
	
	//private byte[] moduleNameB = AppStaticConfigs.stringRedisSerializer.serialize(moduleName); 
	
	private List<SmsEntityForMt> entities = null;
	
	private CyclicBarrier barrier = null;
	
	public AttachMsgFilterTask(List<SmsEntityForMt> entities, CyclicBarrier barrier) {
		this.entities = entities;
		this.barrier = barrier;
	}
	
	@Override
	public void run() {
		
		if (entities == null) return;
		
		long start = System.currentTimeMillis();
		
		for (SmsEntityForMt entity:entities) {
			
			String seqNum = StringUtils.toString(entity.getSerialno()) + ":" + StringUtils.toString(entity.getPhoneNumber());
			String content = StringUtils.toString(entity.getContent());
			String srvNode = StringUtils.toString(entity.getSrvNode());
			String serviceCode = StringUtils.toString(entity.getServiceCode());
			
			String moSm = "";
			Integer timeLen = 0;
			String value = "";
			
			Set<String> set = AppStaticConfigs.configAffixMap.get(srvNode + ":" + serviceCode);
			if (set != null) {
				for (String str:set) {
					value = StringUtils.toString(str); break;
				}
			}
			
			if (StringUtils.isEmpty(value)) {
				Set<String> set2 = AppStaticConfigs.configAffixMap.get(srvNode + ":");
				if (set2 != null) {
					for (String str:set2) {
						value = StringUtils.toString(str); break;
					}
				}
			}
			
			if (StringUtils.isEmpty(value)) {
				Set<String> set3 = AppStaticConfigs.configAffixMap.get(":" + serviceCode);
				if (set3 != null) {
					for (String str:set3) {
						value = StringUtils.toString(str); break;
					}
				}
			}
			
			if (StringUtils.isEmpty(value)) {
				Set<String> set4 = AppStaticConfigs.configAffixMap.get(":");
				if (set4 != null) {
					for (String str:set4) {
						value = StringUtils.toString(str); break;
					}
				}
			}
			
			if (!StringUtils.isEmpty(value)) {
				String[] split = value.split(":");
				String affixSm = split[0];
				moSm = split[1];
				timeLen = StringUtils.toInteger(split[2], 24, true);
				
				StringBuffer buffer = new StringBuffer();
				buffer.append(content);
				buffer.append("[");
				buffer.append(affixSm);
				buffer.append("]");
				
				entity.setContent(buffer.toString());
				entity.setConSm("1");
				
				StringBuffer buffer2 = new StringBuffer();
				buffer2.append(seqNum);
				buffer2.append(":");
				buffer2.append(entity.getSrvNode());
				buffer2.append(":");
				buffer2.append(entity.getServiceCode());
				buffer2.append(":");
				buffer2.append(entity.getSendPort());
				buffer2.append(":");
				buffer2.append(moSm);
				
				long time = timeLen * 3600000;
				
				long expiryTime = System.currentTimeMillis() + time;
				
				entity.setDealResultCodeMap(moduleName, DealResult.RESULT11);
				entity.setAttachInfo(buffer2.toString());
				entity.setAttachExpireTime(expiryTime);
				
				logger.debug("[序列号:用户号码][" + entity.getSerialno() + ":" + entity.getPhoneNumber() + "]被附加短信过滤器捕获,附加内容完成。");
			
			} else {
				entity.setDealResultCodeMap(moduleName, DealResult.RESULT0);
			}
		}
		
		long end = System.currentTimeMillis();
		
		logger.debug("附加短信过滤执行完毕,共耗时:" + (end - start) + "毫秒");
		
		try {
			barrier.await();
		} catch (InterruptedException | BrokenBarrierException e) {
			e.printStackTrace();
		}
	}

}
